package com.itheima.health.service.impl;

import com.alibaba.druid.sql.dialect.odps.ast.OdpsAnalyzeTableStatement;
import com.google.gson.internal.$Gson$Preconditions;
import com.itheima.health.dao.GenderDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.GenderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@DubboService
@Slf4j
public class GenderServiceImpl implements GenderService {
    @Autowired
    private GenderDao genderDao;

    /**
     * 男女比例
     * @return
     */
    @Override
    public List<Map<String, Object>> countByGender() {

        return genderDao.countBygGender();
    }

    /**
     * 会员年龄统计
     * @return
     */
    @Override
    public List<Map<String,Object>> findByMember() {
        //自定义变量
        //0-18岁数量
        int a = 0;
        //19-30岁数量
        int b = 0;
        //31-45岁数量
        int c = 0;
        //45岁以上数量
        int d = 0;

        //会员的生日(出生年月)
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String year1 = simpleDateFormat.format(new Date());
        Integer jinnian = Integer.valueOf(year1);
        List<Date> member = genderDao.findByMember();
        log.info("[会员生日]data{}",member);
        for (Date date : member) {
            Integer sengri = Integer.valueOf(simpleDateFormat.format(date));

            if ((jinnian-sengri)<=18&&(jinnian-sengri)>=0){
                a++;
            }else if ((jinnian-sengri)>=19&&(jinnian-sengri)<=30){
                b++;
            }else if ((jinnian-sengri)>=31&&(jinnian-sengri)<=45){
                c++;
            }else if ((jinnian-sengri)>45){
                d++;
            }

        }

        //0-18岁
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","0-18岁");
        map.put("value",a);
        //18-30岁
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("name","19-30岁");
        map1.put("value",b);
        //30-45岁
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("name","31-45岁");
        map2.put("value",c);
        //45岁以上
        HashMap<String, Object> map3 = new HashMap<>();
        map3.put("name","45岁以上");
        map3.put("value",d);

        List<Map<String, Object>> list = new ArrayList<>();
        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);

        return list;
    }


}
